/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.panel.movie.edit;

import hu.glassy.core.model.Code;
import hu.glassy.core.model.Movie;
import hu.glassy.core.model.MovieRole;
import hu.glassy.core.model.Person;
import hu.glassy.core.service.CodeService;
import hu.glassy.core.service.MovieService;
import hu.glassy.core.service.PersonService;
import hu.glassy.web.panel.code.CodeSelectPanel;
import hu.glassy.web.panel.common.CustomPanel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexander Molnár <molnar.alexander@sonrisa.hu>
 */
public final class MovieStaffPanel extends CustomPanel<Movie>
{

  @SpringBean
  private MovieService movieService;

  @SpringBean
  private CodeService codeService;

  @SpringBean
  private PersonService personService;

  private Person selectedPerson;

  public MovieStaffPanel(String id, IModel<Movie> movie)
  {
    super(id, movie);

    List<IColumn> columns = new ArrayList<IColumn>();
    columns.add(new PropertyColumn<String, String>(new ResourceModel("movie.staff.edit.list.role"), "role.name"));
    columns.add(new PropertyColumn<String, String>(new ResourceModel("movie.staff.edit.list.name"), "person.name"));
    DataTable staffTable = new DataTable("staffTable", columns, new StaffDataProvider(), 10);
    add(staffTable);

    // Create the form
    MovieRole movieRole = new MovieRole();
    movieRole.setMovie(movie.getObject());
    final Form<MovieRole> form = new StatelessForm<MovieRole>("movieStaffForm", Model.of(movieRole))
    {

      @Override
      protected void onSubmit()
      {
        super.onSubmit();

        this.getModelObject().setPerson(selectedPerson);
        movieService.addStaff(this.getModelObject());
        success(getString("movie.edit.save.success"));
      }

    };
    add(form);

    Code rootCode = codeService.getCodeByNameAndParent("Movie roles", null);
    CodeSelectPanel roleSelect = new CodeSelectPanel("roleSelect", rootCode)
    {

      @Override
      public void onChange(Code selectedCode)
      {
        form.getModelObject().setRole(selectedCode);
      }

    };
    form.add(roleSelect);

    DropDownChoice<Person> personSelect = new DropDownChoice<Person>("personSelect");
    personSelect.setModel(new PropertyModel<Person>(this, "selectedPerson"));
    personSelect.setChoices(personService.listPersons());
    personSelect.setChoiceRenderer(new ChoiceRenderer<Person>("name", "id"));
    form.add(personSelect);
  }

  private class StaffDataProvider implements IDataProvider<MovieRole>
  {

    @Override
    public Iterator<? extends MovieRole> iterator(long first, long count)
    {
      return movieService.getStaff(MovieStaffPanel.this.getMainObject(), first, count).iterator();
    }

    @Override
    public long size()
    {
      return movieService.countStaff(MovieStaffPanel.this.getMainObject());
    }

    @Override
    public IModel<MovieRole> model(MovieRole object)
    {
      return Model.of(object);
    }

    @Override
    public void detach()
    {

    }

  }
}
